package name.lzc.thread.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lzc
 * @date 2021/3/6 17:19
 * See More: https://github.com/lzco, https://gitee.com/lzco
 */
@Configuration
@EnableConfigurationProperties(ThreadPoolProperties.class)
public class ThreadPoolAutoConfiguration {

    @Autowired
    private ThreadPoolProperties threadPoolProperties;

    @Bean
    @ConditionalOnClass(ThreadPoolExecutor.class)
    @ConditionalOnProperty(prefix = "lzc", value = "enabled", havingValue = "true", matchIfMissing = true)
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(threadPoolProperties.getCorePoolSize(),
                threadPoolProperties.getMaximumPoolSize(), 0,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
    }
}
